package com.jwell.panprovider.ui.adapter

import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.panprovider.R
import com.jwell.panprovider.entity.InlineBean
import com.jwell.panprovider.entity.SearchBen
import com.jwell.panprovider.entity.SearchDriverBean
import kotlinx.android.synthetic.main.item_search_layout.view.*

/**
 * Created by 76577 on 2018/4/11.
 * 搜索适配器
 */
class SearchAdapter constructor(list: List<Any>) : RecyclerView.Adapter<SearchAdapter.ViewHolder>() {

    private var list = list
    private var onItemOnClickListener: OnItemOnClickListener? = null
    override fun getItemCount(): Int {
        return list.size
    }


    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_search_layout, parent, false)
        return ViewHolder(view)
    }

    override fun getItemViewType(position: Int): Int {
        return super.getItemViewType(position)

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            //普通选择
            if (list[position] is SearchBen) {
                ll_normal.visibility = View.VISIBLE
                ll_contract.visibility = View.GONE
                image.visibility = View.GONE
                text.text = (list[position] as SearchBen).orgName ?: ""
                if ((list[position] as SearchBen).isCheck) {
                    text_check.visibility = View.VISIBLE
                    text.setTextColor(holder.itemView.resources.getColor(R.color.green))
                } else {
                    text_check.visibility = View.GONE
                    text.setTextColor(holder.itemView.resources.getColor(R.color.black))
                }
                holder.itemView.setOnClickListener {
                    onItemOnClickListener?.onClick(position)
                    for (data in list) {
                        if (data is SearchBen) {
                            data.isCheck = false
                        }
                    }
                    (list[position] as SearchBen).isCheck = true
                    notifyDataSetChanged()
                }
            }
            //选择司机
            if (list[position] is SearchDriverBean) {
                ll_normal.visibility = View.VISIBLE
                ll_contract.visibility = View.GONE
                image.visibility = View.VISIBLE
                var str1 = (list[position] as SearchDriverBean).driverName ?: ""
                var str2 = (list[position] as SearchDriverBean).driverPhone ?: ""
                text.text = str1 + "/" + str2
                if ((list[position] as SearchDriverBean).isCheck) {
                    text_check.visibility = View.VISIBLE
                    text.setTextColor(holder.itemView.resources.getColor(R.color.green))
                } else {
                    text_check.visibility = View.GONE
                    text.setTextColor(holder.itemView.resources.getColor(R.color.black))
                }
                holder.itemView.setOnClickListener {
                    onItemOnClickListener?.onClick(position)
                    for (data in list) {
                        if (data is SearchDriverBean) {
                            data.isCheck = false
                        }
                    }
                    (list[position] as SearchDriverBean).isCheck = true
                    notifyDataSetChanged()

                }
            }
            //选择合同号
            if (list[position] is InlineBean) {
                ll_normal.visibility = View.GONE
                ll_contract.visibility = View.VISIBLE
                var data = (list[position] as InlineBean)
                text_goodsName.text = data.mtrL_NAME ?: ""
                text_contractNum.text = data.mtrL_ORDER_NO ?: ""
                text_carrierName.text = data.supplieR_NAME ?: ""
                when (data.checked) {
                    true -> checkbox.isChecked = true
                    else -> checkbox.isChecked = false
                }
                checkbox.setOnClickListener {
                    when (data.checked) {
                        true -> data.checked = false
                        else -> data.checked = true
                    }
                }
                editText.addTextChangedListener(object : TextWatcher {
                    override fun afterTextChanged(s: Editable?) {
                        s?.isEmpty()?.let {
                            if (it) {
                                data.carNum = 0
                                return
                            }
                        }
                        s?.toString()?.toInt()?.let {
                            data.carNum = it
                        }
                    }

                    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                    }

                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                    }
                })
            }
            when (position) {
                list.size - 1 -> v_line.visibility = View.GONE
                else -> v_line.visibility = View.VISIBLE
            }
        }

    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    public interface OnItemOnClickListener {
        fun onClick(position: Int)
    }

    public fun setOnItemOnClickLisenter(listener: OnItemOnClickListener) {
        this.onItemOnClickListener = listener
    }
}